﻿namespace Bango.WindowsPhone7.TestApp
{
	public partial class MainPage : Microsoft.Phone.Controls.PhoneApplicationPage
	{
		Microsoft.Phone.Controls.WebBrowser _wbUserAgent;
		System.String _userAgent = System.String.Empty;
		System.DateTime _eventStartTime;
		// Constructor
		public MainPage()
		{
			InitializeComponent();
			lblTime.Text = (App.Current as TestApp.App).LoadTimeText;
			_wbUserAgent = new Microsoft.Phone.Controls.WebBrowser();
			_wbUserAgent.IsScriptEnabled = true;
			_wbUserAgent.Visibility = System.Windows.Visibility.Collapsed;
			_wbUserAgent.ScriptNotify += new System.EventHandler<Microsoft.Phone.Controls.NotifyEventArgs>(_wbUserAgent_ScriptNotify);
			ContentPanel.Children.Add(_wbUserAgent);
		}

		private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			Reset();
		}

		private void Reset()
		{
			txtEventName.Text = "WP7 Event";
			txtEventDetail.Text = "WP7 Event Detail";
			txtEventValue.Text = "WP7 Event Value";
			txtParamName1.Text = "BGO_SOURCE";
			txtParamValue1.Text = "WP7 SOURCE";
			txtParamName2.Text = "BGO_TERM";
			txtParamValue2.Text = "WP7 TERM";
			lblTime.Text = System.String.Empty;
			lblMemory.Text = System.String.Empty;
			ShowMemory();
		}

		private void Clear()
		{
			txtEventName.Text = System.String.Empty;
			txtEventDetail.Text = System.String.Empty;
			txtEventValue.Text = System.String.Empty;
			txtParamName1.Text = System.String.Empty;
			txtParamValue1.Text = System.String.Empty;
			txtParamName2.Text = System.String.Empty;
			txtParamValue2.Text = System.String.Empty;
		}

		private System.String GetEventName()
		{
			return txtEventName.Text.Trim();
		}

		private System.String GetEventValue()
		{
			return txtEventValue.Text.Trim();
		}

		private System.String GetEventDetail()
		{
			return txtEventDetail.Text.Trim(); ;
		}

		private System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<System.String, System.String>> GetEventParameters()
		{
			System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<System.String, System.String>> eventParameters;
			eventParameters = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<System.String, System.String>>();

			if (txtParamName1.Text.Trim().Length > 0 || txtParamName2.Text.Trim().Length > 0)
			{
				if (txtParamName1.Text.Trim().Length > 0)
				{
					eventParameters.Add(new System.Collections.Generic.KeyValuePair<System.String, System.String>(txtParamName1.Text.Trim(), txtParamValue1.Text.Trim()));
				}

				if (txtParamName2.Text.Trim().Length > 0)
				{
					eventParameters.Add(new System.Collections.Generic.KeyValuePair<System.String, System.String>(txtParamName2.Text.Trim(), txtParamValue2.Text.Trim()));
				}
			}
			return eventParameters;
		}

		private void chkUserAgent_Checked(object sender, System.Windows.RoutedEventArgs e)
		{
			System.String html = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN""><html><head><script language=""JavaScript"" type=""text/JavaScript"">function printUA() {window.external.notify(navigator.userAgent);}</script></head><body onload=""printUA();""></body></html>";

			if (System.String.IsNullOrEmpty(_userAgent))
			{
				_wbUserAgent.NavigateToString(html);
			}
			else
			{
				Bango.WindowsPhone7.SDK.BangoAgent.setUserAgent(_userAgent);
			}
		}

		private void chkUserAgent_Unchecked(object sender, System.Windows.RoutedEventArgs e)
		{
			Bango.WindowsPhone7.SDK.BangoAgent.setUserAgent(System.String.Empty);
		}

		private void btnLiveVideoStart_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;
			
			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStart();
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStart(GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStart(GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStart(GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStart(GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStart(GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "onLiveVideoStart: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnLiveVideoStop_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;

			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStop();
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStop(GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStop(GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStop(GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStop(GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onLiveVideoStop(GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "onLiveVideoStop: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnSearch_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;

			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onSearch();
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onSearch(GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onSearch(GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onSearch(GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onSearch(GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onSearch(GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "onSearch: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnOnDemandVideoStart_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;

			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStart();
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStart(GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStart(GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStart(GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStart(GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStart(GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "onDemandVideoStart: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnOnDemandVideoStop_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;

			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStop();
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStop(GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStop(GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStop(GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStop(GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onDemandVideoStop(GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "onDemandVideoStop: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnPageView_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;

			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onPageView();
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onPageView(GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onPageView(GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onPageView(GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onPageView(GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onPageView(GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "Page View Updated: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnIdle_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;

			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onIdle();
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onIdle(GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onIdle(GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onIdle(GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onIdle(GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onIdle(GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "onIdle: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnResume_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;

			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onResume();
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onResume(GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onResume(GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onResume(GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onResume(GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onResume(GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "onResume: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnLog_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;

			switch (GetOverload())
			{
				case 1:
					Bango.WindowsPhone7.SDK.BangoAgent.onEvent(GetEventName());
					break;
				case 2:
					Bango.WindowsPhone7.SDK.BangoAgent.onEvent(GetEventName(), GetEventValue());
					break;
				case 3:
					Bango.WindowsPhone7.SDK.BangoAgent.onEvent(GetEventName(), GetEventParameters());
					break;
				case 4:
					Bango.WindowsPhone7.SDK.BangoAgent.onEvent(GetEventName(), GetEventValue(), GetEventDetail());
					break;
				case 5:
					Bango.WindowsPhone7.SDK.BangoAgent.onEvent(GetEventName(), GetEventValue(), GetEventParameters());
					break;
				case 6:
				default:
					Bango.WindowsPhone7.SDK.BangoAgent.onEvent(GetEventName(), GetEventValue(), GetEventDetail(), GetEventParameters());
					break;
			}

			lblTime.Text = "Log Made: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnError_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			_eventStartTime = System.DateTime.Now;
			lblTime.Text = System.String.Empty;
			Bango.WindowsPhone7.SDK.BangoAgent.onError("TestError", "This is my test error", System.Reflection.MethodBase.GetCurrentMethod().Name);
			lblTime.Text = "Error Log Made: " + new System.DateTime(System.DateTime.Now.Subtract(_eventStartTime).Ticks).ToString("mm:ss.ffff");
			ShowMemory();
		}

		private void btnReset_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			lblTime.Text = System.String.Empty;
			Reset();
		}

		private void btnClear_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			lblTime.Text = System.String.Empty;
			Clear();
		}

		private void btnPostNow_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			lblTime.Text = System.String.Empty;
			Bango.WindowsPhone7.SDK.BangoAgent.postEvents();
			lblTime.Text = "Posted Events";
			ShowMemory();
		}

		private void btnViewBatch_Click(object sender, System.Windows.RoutedEventArgs e)
		{

			System.String json;
			System.String[] jsonLines;
			try
			{
				json = GetJSON();
				jsonLines = SplitBySize(json, 500);

				System.Diagnostics.Debug.WriteLine(System.Environment.NewLine + "---Start Batch---");
				foreach (System.String jsonLine in jsonLines)
				{
					if (jsonLine != null && jsonLine.Trim().Length > 0)
					{
						System.Diagnostics.Debug.WriteLine(jsonLine.Trim().Replace("\r", System.String.Empty).Replace("\n", System.String.Empty));
					}
				}
				System.Diagnostics.Debug.WriteLine("---End Batch---");
			}
			catch (System.Exception ex)
			{

				System.Windows.MessageBox.Show(ex.ToString(), "Error", System.Windows.MessageBoxButton.OK);
			}
		}

		private System.String[] SplitBySize(System.String inputString, System.Int32 lineLength)
		{
			System.String[] returnValue = null;
			System.Int32 length = inputString.Length;
			System.Int32 words = length / lineLength + (((length % lineLength != 0)) ? 1 : 0);
			System.Int32 j = 0;
			System.Int32 i = 0;
			returnValue = new System.String[words + 1];
			for (i = 0; i <= length; i += lineLength)
			{
				if (i + lineLength > length)
				{
					returnValue[j] = inputString.Substring(i);
				}
				else
				{
					returnValue[j] = inputString.Substring(i, lineLength);
				}
				j = j + 1;
			}
			return returnValue;
		}

		private System.String GetJSON()
		{
			return Bango.WindowsPhone7.SDK.BangoAgent.viewBatch();
		}

		private void _wbUserAgent_ScriptNotify(object sender, Microsoft.Phone.Controls.NotifyEventArgs e)
		{
			
			_userAgent  = e.Value;
			Bango.WindowsPhone7.SDK.BangoAgent.setUserAgent(_userAgent);
		}

		private void ShowMemory()
		{
			System.Decimal deviceTotalMemory = System.Convert.ToInt64(Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceTotalMemory"));
			System.Decimal applicationCurrentMemoryUsage = System.Convert.ToInt64(Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"));
			System.Decimal applicationPeakMemoryUsage = System.Convert.ToInt64(Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage"));

			lblMemory.Text = "Memory Usage: (" + (applicationCurrentMemoryUsage / 1048576).ToString("0.00") + "/" + (deviceTotalMemory / 1048576).ToString("0.00") + ") Peak: " + (applicationPeakMemoryUsage / 1048576).ToString("0.00") + " (MB)";
		}

		private int GetOverload()
		{
			/*
			 * 1 - Only Event Name Provided
			 * 2 - Event name and value provided
			 * 3 - Only Event Name And Parameters Provided
			 * 4 - Event name, value and detail provided
			 * 5 - Event name, value and event parameters provided
			 * 6 - Event name, value, detail and event parameters provided
			 */

			System.String eventValue = GetEventValue();
			System.String eventDetail = GetEventDetail();
			System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<System.String, System.String>> eventParameters = GetEventParameters();

			if (System.String.IsNullOrEmpty(eventValue) && eventParameters.Count == 0)
			{
				return 1;
			}

			if (!System.String.IsNullOrEmpty(eventValue) && System.String.IsNullOrEmpty(eventDetail) && eventParameters.Count == 0)
			{
				return 2;
			}

			if (System.String.IsNullOrEmpty(eventValue) && eventParameters.Count != 0)
			{
				return 3;
			}

			if (!System.String.IsNullOrEmpty(eventValue) && !System.String.IsNullOrEmpty(eventDetail) && eventParameters.Count == 0)
			{
				return 4;
			}

			if (!System.String.IsNullOrEmpty(eventValue) && System.String.IsNullOrEmpty(eventDetail) && eventParameters.Count != 0)
			{
				return 5;
			}

			if (!System.String.IsNullOrEmpty(eventValue) && !System.String.IsNullOrEmpty(eventDetail) && eventParameters.Count != 0)
			{
				return 6;
			}

			return 1;

		}
		
	}
}